$(function () {
    $("#top-html").load("topfile.html", function () {
        $("#nav-info-1").text("新闻中心");
        $("#nav-info-2").text("NEWS  CENTER");
        $("#location-2").text("新闻中心");
    });
    $("#foot-html").load("footfile.html");
    myajax("/getnewscategorysurvival.json", "get", {}, function (result) {
        if (result.status == 200) {
            let pcate_html = '';
            for (let pcate of result.data) {
                pcate_html += '<div ncid="' + pcate.id + '" onclick="ncateClick(this)">' + pcate.name + '</div>';
            }
            $(".newscenter-content-nav-").html(pcate_html);

            $(".newscenter-content-nav->div").hover(function () {
                checkflag = ($(this).css("background-color") == "rgb(0, 179, 204)");
                $(this).css("background-color", "#00b3cc");

            }, function () {
                if (!checkflag)
                    $(this).css("background-color", "#07192f");
            });


            $(".newscenter-content-nav->div:first-child").trigger("click");
        }
    });
});
var checkflag;
var pagebox;

function ncateClick(obj) {
    checkflag = true;
    $(".newscenter-content-nav->div").css("background-color", '#07192f');
    $(obj).css("background-color", '#00b3cc');
    var cate_id = $(obj).attr("ncid");

    if (pagebox != null) {
        pagebox.destroy();
    }

    $(".newscenter-content").html('<div class="newscenter-content-news"></div><div id="news-page" class="page"></div>');

    $(".newscenter-content-news").empty();

    myajax("/getnewscountbycateid.json", "get", {"cate_id": cate_id}, function (result) {
        if (result.status == 200) {
            pagebox = new CustomPagination('#news-page', {
                total: result.data,//总页数
                changePage: function (pageNum) {//切换页码成功回调
                    myajax("/getnewsbycateid.json", "get", {"cate_id": cate_id, "pagenum": pageNum}, function (res) {
                        if (res.status == 200) {
                            let news_html = '';
                            for (let news of res.data) {
                                let ymd = news.newsdate.split("-");
                                news_html += '<div>' +
                                    '            <div class="newscenter-content-news-date">' +
                                    '                <p>' + ymd[2] + '</p>' +
                                    '                <p>' + ymd[0] + '-' + ymd[1] + '</p>' +
                                    '            </div>' +
                                    '            <div class="newscenter-content-news-data">' +
                                    '                <div nid="' + news.id + '" onclick="show_news_info(this)">' + news.title + '</div>' +
                                    '                <div>' + news.summary + '</div>' +
                                    '            </div>' +
                                    '        </div>';
                            }
                            $(".newscenter-content-news").html(news_html);
                        }
                    });
                }
            });
        }
    });
}

function show_news_info(obj) {
    $("#newscenter-content-id").load("newsinfo.html", function () {
        myajax("/getnewsallinfo.json", "get", {"id": $(obj).attr("nid")}, function (result) {
            if (result.status == 200) {
                let news = result.data.news;
                let newsinfo = result.data.newsInfo;
                $("#title").html(news.title);
                $("#summary").html(news.summary);
                $("#newsdate").html(news.newsdate);

                $("#source").html(newsinfo.source);
                $("#author").html(newsinfo.author);
                $("#numviews").html(newsinfo.numviews);
                $("#content").html(newsinfo.content);
            }
        });
    });
}